package com.market.category.controller;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.market.category.dao.CategoryDao;
import com.market.category.domain.CategoryCommand;
import com.market.util.CatePagingUtil;

@Controller
public class CateListController {
	//private Logger log = Logger.getLogger(this.getClass());
	private int pageSize = 10;
	private int blockCount = 10;
	
	@Autowired
	private CategoryDao categoryDao;
	
	@RequestMapping("categoryList.do")
	public ModelAndView process(@RequestParam(value="pageNum",defaultValue="1")int currentPage,
			@RequestParam(value="keyField",defaultValue="market")String keyField,
			@RequestParam(value="searchMarket",defaultValue="all")String keyWord){
		String pagingHtml = "";
		
		/*
		if(log.isDebugEnabled()){
			log.debug("currentPage : " + currentPage);
			log.debug("keyField : " + keyField);
			log.debug("keyWord : " + keyWord);
		}*/
		
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("keyWord", keyWord);	
		
		int count = categoryDao.getRowCount(map);
		
		CatePagingUtil page = new CatePagingUtil(keyField, keyWord,
				currentPage, count, pageSize, blockCount, "categoryList.do");
		
		pagingHtml = page.getPagingHtml().toString();//???
		
		map.put("start", page.getStartCount());
		map.put("end", page.getEndCount());
		
		List<CategoryCommand> list = null;
		if(count > 0){
			list = categoryDao.list(map);
		}else{
			list = Collections.emptyList();
		}
		
		int number = count-(currentPage-1)*pageSize;//珥��꾩떆���섏씠吏�
		
		ModelAndView mav = new ModelAndView();
		mav.setViewName("categoryList");
		mav.addObject("count", count);//珥앷� ��
		mav.addObject("list", list);//�꾩떆��紐⑸줉
		mav.addObject("pagingHtml", pagingHtml);//�섏씠吏�紐⑸줉
		mav.addObject("number", number);
		mav.addObject("pageNum",currentPage);
		mav.addObject("market",keyWord);		
		return mav;
	}
}
